#    Tichy
#
#    copyright 2008 Guillaume Chereau (charlie137@gmail.com)
#
#    This file is part of Tichy.
#
#    Tichy is free software: you can redistribute it and/or modify it
#    under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Tichy is distributed in the hope that it will be useful, but
#    WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Tichy.  If not, see <http://www.gnu.org/licenses/>.


from widget import Widget


class Stack(Widget):

    def __init__(self, parent, **kargs):
        super(Stack, self).__init__(parent, **kargs)

    @property
    def top(self):
        return self.children[-1]

    def put_on_top(self, child):
        if isinstance(child, int):
            child = self.children[child]
        assert child in self.children
        self.children.remove(child)
        self.children.append(child)
        self.need_redraw(self.rect)

    def _full_draw(self, painter):
        if not self.children:
            return
        painter.move(self.top.pos)
        self.top._full_draw(painter)
        painter.umove(self.top.pos)

    def sorted_children(self):
        return [self.top] if self.children else []
